package com.ejoysoft.wlanyz.business;


import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

import com.ejoysoft.common.DbConnect;
import com.ejoysoft.common.Format;
import com.ejoysoft.common.Globa;
import com.ejoysoft.common.UID;

public class Ap {
	private Globa globa;
    private DbConnect db;


    public Ap() {
    }

    public Ap(Globa globa) {
        this.globa = globa;
        db = globa.db;
    }

    public Ap(Globa globa, boolean b) {
        this.globa = globa;
        db = globa.db;
        if (b) globa.setDynamicProperty(this);
    }

    String strTableName = "RM_DEVICE";
    String strTableName1 = "AC_CLASS_ASSOCIATE";
    String strTableName2 = "NHM_AP_RSSI_KPI_HOUR";
    String strTableName3 = "NHM_CHANNEL_CHANGE";
    String strTableName4 = "ac_lat_lng";
    
   
    
//返回单条记录
    public Ap show(String where) {
        try {
            String strSql = "select * from  " + strTableName + "  ".concat(where);
            ResultSet rs = db.executeQuery(strSql);
            if (rs != null && rs.next())
                return load(rs, true);
            else
                return null;
        } catch (Exception ee) {
            return null;
        }
    }

    public boolean update() {
        try {
        	String strIdSql = "select b.Strid from " + strTableName+ " a, " + strTableName1 + " b  where a.hotspot = b.classtwoac and a.mac_addr = '" + apMac +"'";
        	ResultSet rs = db.executeQuery(strIdSql);
        	System.out.println(strIdSql);
        	String strId = null;
        	 if (rs!=null&&rs.next()){
        		 strId = rs.getString(1);
        	 }
             rs.close();
             String strId2 = UID.getID();//如果strTableName4中可以update则更新。
             System.out.println(strId+"====+++");
             if(strId != null && !"".equals(strId)){
            	 String strSql2 = "UPDATE "+ strTableName4 + " SET phonelocation = ?, strCreator = ?, dtCreateTime = to_date(?,'yyyy-MM-dd hh24:mi:ss') where acname= ? ";
            	 db.prepareStatement(strSql2);
            	 System.out.println(strSql2+"====");
                 db.setString(1, apLatlng);
                 db.setString(2, "system");
                 db.setString(3, Format.getDateTime()); 
                 db.setString(4, strId);
//                 db.executeUpdate();
                 if (db.executeUpdate() > 0) {
                	 System.out.println("修改成功！！！");
					Globa.logger0("修改热点信息", "System", "", strSql2, "热点信息", "");
					return true;
				}
               //如果strTableName4中不可以update则上传。
                 else {
                	 String strSql = "INSERT INTO   " + strTableName4 + "   (phonelocation , strCreator, dtCreateTime  , acname , strid) VALUES (?,?,to_date(?,'yyyy-MM-dd hh24:mi:ss'),?,?)";
                     db.prepareStatement(strSql);
                     db.setString(1, apLatlng);
                     db.setString(2, "system");
                     db.setString(3, Format.getDateTime()); 
                     db.setString(4, strId);
                     db.setString(5, strId2);
//                     db.executeUpdate();
                     if (db.executeUpdate() > 0) {
    					System.out.println("插入成功！！！");
    					Globa.logger0("修改热点信息", "System", "", strSql, "热点信息", "");
    					return true;
    				}
				}
            	 
             } 
            
            return false;
        } catch (Exception e) {
            System.out.println("修改信息时出错：" + e);
            e.printStackTrace();
            return false;
        }finally{
        	try{
        		if(db!=null){
        			db.closeCon();
        			db=null;
        		}
        	}catch(Exception e){
        		e.printStackTrace();
        	}
        }
    }
  //查询符合条件的记录总数
    public int getCount(String where) {
        int count = 0;
        try {
          //  String sql = "SELECT count(DEVICE_ID) FROM " + strTableName + " a  ";
        	  String sql = "SELECT count(a.DEVICE_ID) FROM  " + 
				  strTableName + " a LEFT JOIN "  + strTableName3 + " c ON a.DEVICE_ID=c.DEVICE_ID " + 
   				 "LEFT JOIN " + strTableName2 + " b ON a.DEVICE_ID=b.DEVICE_ID ";
            if (where.length() > 0) {
                // where = where.toLowerCase();
                if (where.indexOf("order") > 0)
                    where = where.substring(0, where.lastIndexOf("order"));
                sql = String.valueOf(sql) + String.valueOf(where);
            }
            ResultSet rs = db.executeQuery(sql);
            if (rs.next())
                count = rs.getInt(1);
            rs.close();
            return count;
        } catch (Exception ee) {
            ee.printStackTrace();
            System.out.println(ee);          
            return count;
        }
    }



    //记录集转化为对象
    public Ap load(ResultSet rs, boolean isView) {
        Ap theBean = new Ap();
        DbConnect dbConnect = new DbConnect();
        try {
        	
        	//theBean.setstrId(rs.getString(1));
            theBean.setApDevId(rs.getString(1));	
            theBean.setApCity(rs.getString(2));
            theBean.setApCounty(rs.getString(3));
            theBean.setHotSpot(rs.getString(4));
            theBean.setApName(rs.getString(5));
        //    theBean.setApArgRssi(rs.getDouble("apArgRssi"));
       //    theBean.setApArgRsn(rs.getDouble("apArgRsn"));
            theBean.setApRssi(rs.getDouble(6));
            theBean.setApRsn(rs.getDouble(7));
            theBean.setApIp(rs.getString(8));
            theBean.setApQuality(rs.getDouble(9));
            String sql = " select strid from "+strTableName1+"  where classtwoac ='"+theBean.getHotSpot()+"' " ; 
            ResultSet rs1 = dbConnect.executeQuery(sql);
            if(rs1!=null && rs1.next())
            { 
            	theBean.setstrId(rs1.getString(1));
                rs1.close();
            }
        } catch (Exception e) {
            e.printStackTrace();      
        }
        dbConnect.closeCon();
        return theBean;
    }

  //列表记录集
    public Vector list(String where, int startRow, int rowCount) {
        Vector beans = new Vector();
        try {
          /*  String sql = "SELECT a.DEVICE_ID,a.CITY,a.COUNTY,a.HOTSPOT,a.DEVICE_NAME,b.RSN,b.RSSI,a.IP_ADDR,c.QUALITY  FROM  " + 
                         strTableName + " a LEFT JOIN " + strTableName2 + " b ON a.DEVICE_ID=b.DEVICE_ID " + "LEFT JION" +
                          strTableName3 + " c ON a.DEVICE_ID=c.DEVICE_ID " ;*/
           String sql = "SELECT a.DEVICE_ID,a.CITY,a.COUNTY,a.HOTSPOT,a.DEVICE_NAME,b.RSN,b.RSSI,a.IP_ADDR,c.QUALITY  FROM  " +
                        strTableName+" a  LEFT JOIN "  + strTableName3 + " c ON a.DEVICE_ID=c.DEVICE_ID " + 
           				 "LEFT JOIN " + strTableName2 + " b ON a.DEVICE_ID=b.DEVICE_ID ";
            if (where.length() > 0)
                sql = String.valueOf(sql) + String.valueOf(where);
            Statement s = db.getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            if (startRow != 0 && rowCount != 0)
                s.setMaxRows((startRow + rowCount) - 1);
//            System.out.println(""+sql);       
            ResultSet rs = s.executeQuery(sql);
            if (rs != null && rs.next()) {
                if (startRow != 0 && rowCount != 0)
                    rs.absolute(startRow);
                do {
                    Ap theBean = new Ap();
                    theBean = load(rs, false);
                    beans.addElement(theBean);
                } while (rs.next());
            }
            rs.close();
            s.close();
        } catch (Exception ee) {
            ee.printStackTrace();
        }
        return beans;
    }
    //列表记录集
    public Vector<String> listDeviceId(String where) {
        Vector<String>  beans = new Vector<String>();
        try {
          String sql = "SELECT device_id from  "+ strTableName+ " ";
            if (where.length() > 0)
                sql = String.valueOf(sql) + String.valueOf(where);
            ResultSet rs = db.executeQuery(sql);
            if (rs != null && rs.next()) {
               do { 
                    beans.add(rs.getString("device_Id"));
                } while (rs.next());
            }
            rs.close();
        } catch (Exception ee) {
            ee.printStackTrace();
        }
        db.closeCon();
        return beans;
        
    }
    public Map<String, ArrayList<String>>  getAllclassTwoByName(){
    	Map<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
    	String sql = "select distinct(hotspot) from " + strTableName ;
    	try {
    		ResultSet rs = db.executeQuery(sql);
        	if (rs != null && rs.next()) {
                do { 
                	 ArrayList<String> list = new ArrayList<String>();
                     String hotspot = rs.getString(1);
                     String sql1 = "select distinct(device_name) from " + strTableName + " where hotspot like " + "'%" + hotspot +"%' ";
                     ResultSet rs1 = db.executeQuery(sql1);
                     if(rs1 !=null && rs1.next()){
                    	 do{
                    		 String apName = rs1.getString(1);
                        	 list.add(apName);
                    	 }while(rs1.next());
                     }
                     rs1.close();
                     //for(int i=0;i<list.size();i++)System.err.println("----------------------i:"+ hotspot+"::"+list.get(i));
                     map.put(hotspot, list);
                 } while (rs.next());
             }
             rs.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		Iterator it = map.keySet().iterator();
		  while(it.hasNext()){
		   String key = (String) it.next();
		   ArrayList<String> value = map.get(key);
		  } 
		  
    	return map;
    }
    private String strId;//	
    private String apDevId;//	
    private String apCity;//		
    private String apcounty;//	
    private String hotSpot;//	
    private String apName;
    private double apArgRssi;
    private double apArgRsn;
    private double apQuality;
    private double apRsn;
    private double apRssi;
    private String apIp;
    private String apMac;
    private String apLatlng;
    
    public String getApLatlng() {
		return apLatlng;
	}

	public void setApLatlng(String apLatlng) {
		this.apLatlng = apLatlng;
	}

	public String getstrId() {
        return strId;
    }
    public void setstrId(String strId) {
        this.strId = strId;
    }
    public String getApDevId() {
        return apDevId;
    }
    public void setApDevId(String apDevId) {
        this.apDevId = apDevId;
    }

    public String getApCity() {
        return apCity;
    }

    public void setApCity(String apCity) {
        this.apCity = apCity;
    }
    
    public String getApCounty() {
		return apcounty;
	}
    
    public void setApCounty(String apcounty){
    	 this.apcounty = apcounty;
    }
    
    public String getApName() {
		return apName;
	}
    
    public void setApName(String apName){
    	this.apName = apName;
    }
    
    public String getHotSpot() {
		return hotSpot;
	}
    
    public void setHotSpot(String hotSpot){
    	this.hotSpot = hotSpot;
    }
    public double getApArgRssi() {
		return apArgRssi;
	}
    
    public void setApArgRssi(double apArgRssi) {
		this.apArgRssi = apArgRssi;
	}
    public double getApArgRsn() {
        return apArgRsn;
    }

    public void setApArgRsn(double apArgRsn) {
        this.apArgRsn = apArgRsn;
    }

    public double getApQuality() {
        return apQuality;
    }

    public void setApQuality(double apQuality) {
        this.apQuality = apQuality;
    }
    public double getApRsn() {
        return apRsn;
    }

    public void setApRsn(double apRsn) {
        this.apRsn = apRsn;
    }
    public double getApRssi() {
        return apRssi;
    }
    public void setApRssi(double apRssi) {
        this.apRssi = apRssi;
    }
    public String getApIp() {
        return apIp;
    }
    public String getApMac() {
		return apMac;
	}

	public void setApMac(String apMac) {
		this.apMac = apMac;
	}

	public void setApIp(String apIp) {
        this.apIp = apIp;
    }

}